// JavaScript Document

function aaa(){
}

function  get_post_intro_success(d){
	
                    var txt = MI.string.html(String(d)),
                        txt_arr = txt.split('\n'),
                        txt_arr_new =[];//txt.replace(/\n/g, '<br>');
                    for (var i=0,j=txt_arr.length; i<j; i++){
                        (txt_arr[i] != '') && txt_arr_new.push(txt_arr[i])
                    }
                    $('.activeview .movie_page .movie_detail').html(txt_arr_new.join('<br>'));
                  }
//｛“category”:5}
function get_post_intro(txt_file)
{
	
	if(	window.location.host=="")  //cs envi
	{
	var mp="$method=aaaCms.CmsImpLocalFileVer.get_post_intro&$callback=get_post_intro_success&param="+txt_file;
	 //	alert(mp);
		HRE.exe(mp,get_post_intro_success);	
	}
		
			txt_file=encodeURIComponent(txt_file);
			 	 var dsl1="new com.attilax.movcms.CmsImpLocalFileVer().get_post_intro('@txt_file@')";
		 dsl1=dsl1.replace("@txt_file@",txt_file);
		 
		 	var xhr=threadLocalParamMap.xhr;
		  xhr.req({dsl:dsl1},get_post_intro_success);
	
}
function setPostSingleInfo(data)
{
	
	// function(data){  //qb2 jeig sh invoke get_post_single,then callback
			
			//this fun is invoke by get_post() in cms api file.
		//	alert(data);
			
			//ati pb28  add try
		 
				// get post callback
		//	alert(data);
            data = data || {};
            var rw = data.rows || [];
		//	alert("====== rw.length:"+rw.length);
            var    r = data.rows[0] || {};
			
         var       name = MI.string.html(String(r.material_description)),
                thumb = $approot+'/' +r.thumb,
                file_path = r.file_path,
                detail = r.detail,
                txt_file ='/vdx/' + r.txt_file;
				
			if(inDesktopMode())
			{
				  thumb = "file:///" +r.thumb;
			}
			
		 
					 
//alert("pac1::"+name);
            $('.activeview .movie_page h3 span').text(name);
            $('.activeview .movie_page img').attr('src',thumb);
            //$('.activeview .movie_page .movie_detail').html(MI.string.html(String(detail)));
				//alert("v1");  haox yond sh jeig
            $('.activeview .movie_page .btn_play').attr('url', file_path);
			//ati p89
			  $('.activeview .movie_page .btn_play').attr('mvid', r.file_path);
			  
			  
			
		  
        
	
	
	
}



function get_post_callback_4single(data)
{
//	alert("---get_posts_callback"); 
 	//alert(data);
	data=str2json(data);
	var rows=data.rows;
	var len=getJsonLength(rows);
	for(var cur=0;cur<len;cur++)	 
	{
		var item=rows[cur];
		var urlencode=encodeURIComponent(item.thumb);
		item.thumb="PicReadServlet?src="+urlencode;
		
	}
	try{	
	setPostSingleInfo(data);
	}catch(e)
	{
		showErr(e);	
	}
	try{	
	//get post intro
		  var    r = data.rows[0] || {};
		  get_post_intro(r.txt_file);
	  }catch(e)
	{
		showErr(e);	
	}
}


function get_posts_callback(data)
{
//	alert("---get_posts_callback"); 
 	//alert(data);
	data=str2json(data);
	var rows=data.rows;
	var len=getJsonLength(rows);
	for(var cur=0;cur<len;cur++)	 
	{
		var item=rows[cur];
		var urlencode=encodeURIComponent(item.thumb);
		item.thumb="PicReadServlet?src="+urlencode;
		
	}
	try{	
			var fn=threadLocalParamMap.fn;
			
			var xhr=threadLocalParamMap.xhr
		//  	alert("--fn:"+fn);
			  fn && fn(data);
			  // xhr.abort	(req_id);
	  
	  }catch(e)
	{
		showErr(e);	
	}
}
function get_posts(paramJsonMap)
{
try{	
	var  sql_t = 'material_type';
	var xhr=paramJsonMap.xhr;
	var fn=paramJsonMap.fn;
	var err=paramJsonMap.err;
	var value=paramJsonMap.value; //cate id
	threadLocalParamMap.xhr=xhr;
	threadLocalParamMap.fn=fn;

//com.attilax.api.imoveSqlExcutor.exe
	if(	window.location.host=="")  //cs envi
	{
	   //todox pa6 param use naming params jsonfmt ..not use obj arr
	   //beir param1=xxx param2=xxx param3=xxx 
		var mp="$method=aaaCms.CmsImpLocalFileVer.list_nocache&$callback=get_posts_callback&param="+value;
	// 	alert(mp);
		HRE.exe(mp,get_posts_callback);	
		
		//	var json=urlParams2json(param);
		//	sendNSCommand(json.method,param,callback);
		//		alert("sendNSCommand ok");
		return;
		
	}


         //   xhr.req({http_param: 'select  * from gv_material where '+[sql_t, '=', value].join(' ')+' order by material_description ' }, fn, err);
		 //  String dsl="new com.attilax.movcms.CmsImpLocalFileVer().list_nocache('3')";
		 var dsl1="new com.attilax.movcms.CmsImpLocalFileVer().list_nocache('@cateid@')";
		 dsl1=dsl1.replace("@cateid@",value);
		  xhr.req({dsl:dsl1},get_posts_callback,err);
}catch(e)
{
	showErr(e);	
}
}

/**
for index only 
*/
function get_post( id)
{
	
	try{
		var mid= id;
		var xhr=threadLocalParamMap.xhr;
		var fn=threadLocalParamMap.fn;
		var err=threadLocalParamMap.fn;
		var value=threadLocalParamMap.value;
		var sql='select   *  from gv_material where material_id='+mid+' ';		
		if(	window.location.host=="")  //cs envi
			{
				mid=encodeURIComponent(mid);
				var mp="$method=aaaCms.CmsImpLocalFileVer.get_post_api&$callback=get_posts_callback&param="+mid;
			//	alert("get post mp:"+mp);
				HRE.exe(mp,get_posts_callback);	
				return;
			}else
			 //  xhr.req({http_param:sql }, fn, err);
				 {
					 	 var dsl1="new com.attilax.movcms.CmsImpLocalFileVer().get_post_api('@mid@')";
						 mid=encodeURIComponent(mid);
						dsl1=dsl1.replace("@mid@",mid);
						xhr.req({dsl:dsl1},get_post_callback_4single,err);
					 
				 }
	
	
	}catch(e)
	{
		showErr(e);	
	}
}


function query_posts( keyword)
{
	try{
//	alert(keyword);
	var xhr=threadLocalParamMap.xhr;
var fn=threadLocalParamMap.fn;  //callback
var err=threadLocalParamMap.err;
//var value=threadLocalParamMap.value;

	if(	window.location.host=="")  //cs envi
			{
			//	mid=encodeURIComponent(mid);
				var mp="$method=aaaCms.CmsImpLocalFileVer.searchV2&$callback=get_posts_callback&param="+keyword;
			//	alert("get post mp:"+mp);
				HRE.exe(mp,get_posts_callback);	
				return;
			}
			else
			{
					 var dsl1="new com.attilax.movcms.CmsImpLocalFileVer().searchV2('@mid@')";
						 keyword=encodeURIComponent(keyword);
						dsl1=dsl1.replace("@mid@",keyword);
						xhr.req({dsl:dsl1},get_posts_callback);
//	 xhr.req({http_param: "select   * from gv_material where material_keyword like '"+keyword+"%'  limit 30 "}, fn, err);
	 
			}
	}catch(e)
	{
	showErr(e);	
	}
}